import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { PageTitle } from "./page-title"
import { UserService } from "./user.service"

@Component({
  selector: 'login',
  templateUrl: "./login.component.html",
  styleUrls: ["./login.component.css"]
})
export class LoginComponent implements OnInit {
  account: string
  passwd: string

  constructor(private router: Router,
              private pageTitle: PageTitle,
              private userService: UserService) { }

  ngOnInit(): void {
    this.pageTitle.back = null
    this.pageTitle.title = "登录页"
  }

  login(): void {
    this.userService.login(this.account, this.passwd).then(res => {
      if (!res.error) {
        localStorage.setItem("token", res.data.token)
        this.router.navigate([ "users", "mine", "tasks" ])
        location.reload(true)
      } else if (res.error == "PHONE-OR-PASSWD-ERROR")
        alert("用户名或者密码错")
    })
  }
}
